//
//  LayerTable.cpp
//  lhq
//
//  Created by codin on 14-7-24.
//
//

#include "LayerTable.h"
#include "LayerCard.h"
USING_NS_CC;

/*
static GlobaDataManager *s_response = NULL;
GlobaDataManager* GlobaDataManager::getInstance()
{
    if (s_response == NULL) {
        s_response = new GlobaDataManager();
    }
    return s_response;
}
void GlobaDataManager::setPomeloReponse(PomeloReponse* tRes)
{
	m_res_data=tRes;
}
PomeloReponse * GlobaDataManager::getPomeloReponse()
{
	return m_res_data;
}
*/
Scene* LayerTable::createScene(){
//Scene* LayerTable::createScene(PomeloReponse * tRes) {
    auto scene = Scene::create();
    auto layer = LayerTable::create();
    scene->addChild(layer);
    return scene;
}

bool LayerTable::init(){
    if (!Layer::init()){
        return false;
    }
    
    auto director = Director::getInstance();
    
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();
    
    auto bgSp = Sprite::create("texture/zhuomian.jpg");
    bgSp->setPosition(Vec2(origin.x + visibleSize.width - bgSp->getContentSize().width/2,origin.y + bgSp->getContentSize().height/2));
    this->addChild(bgSp);
    
    return true;
}

void LayerTable::onEnter()
{
	 initView();
	 addPlayerBtns();
	initNet();

	m_layer_card=new LayerCard();
	m_layer_card->setParent(this);

	m_layer_card->setVec(GamePlayer::getInstance()->getCardResource());
	m_layer_card->fixVec();

	this->addChild(m_layer_card);

}
void LayerTable::initView()
{
	SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
	frameCache->addSpriteFramesWithFile(globa_resource[res_icons_plist],globa_resource[res_icons_png]);

	frameCache->addSpriteFramesWithFile(globa_resource[res_cards_plist],globa_resource[res_cards_png]);

	frameCache->addSpriteFramesWithFile(globa_resource[res_operate_btns_plist],globa_resource[res_operate_btns_png]);
}
void LayerTable::addPlayerBtns()
{
	/*
	CCMenu *t_menu=this->getChildByTag(PLAYER_BTN);
	if(t_menu)
	{
		
	}

	for(int location=0;location<PAOHUZI_MAX_PLAYER_COUNT;location++)
	{
		
	}
	*/
}
void LayerTable::initNet()
{
	Pomelo::getInstance()->addListener("onPlayerToOperate",this, callfuncND_selector(LayerTable::onPlayerToOperate));
	Pomelo::getInstance()->addListener("onPlayerOutCard",this, callfuncND_selector(LayerTable::onPlayerOutCard));
	Pomelo::getInstance()->addListener("onPlayerChi",this, callfuncND_selector(LayerTable::onPlayerChi));
	Pomelo::getInstance()->addListener("onPlayerPeng",this, callfuncND_selector(LayerTable::onPlayerPeng));
	Pomelo::getInstance()->addListener("onPlayerPao",this, callfuncND_selector(LayerTable::onPlayerPao));


}

void LayerTable::onPlayerToOperate(cocos2d::CCNode *node, void *resp)
{
	PomeloReponse* ccpomeloresp = (PomeloReponse*)resp;
	showOperateBtns(OPERATE_NULL);
}
void LayerTable::onPlayerOutCard(cocos2d::CCNode *node, void *resp)
{
	//int location=
}
void LayerTable::onPlayerChi(cocos2d::CCNode *node, void *resp)
{

}
void LayerTable::onPlayerPeng(cocos2d::CCNode *node, void *resp)
{

}
void LayerTable::onPlayerPao(cocos2d::CCNode *node, void *resp)
{

}
int LayerTable::positionToLocation(int position)
{
	int index=(PAOHUZI_MAX_PLAYER_COUNT-myposition+position-1)%PAOHUZI_MAX_PLAYER_COUNT;
	if(playerCount==2)
	{
		if(index==0){
			return 1;
		}else if(index==1){
			return 3;
		}
	}else if(playerCount==3){
		if(index==0){
			return 0;
		}else if(index==1){
			return 2;
		}else if(index==2){
			return 3;
		}
        }else if(playerCount==4){
            return index;
        }
}

void LayerTable::showOperateBtns(OPERATE operateCode)
{
	CCSize size = CCDirector::sharedDirector()->getWinSize(); 
	int btnW = 150;
	int right = 100;
	int i=0;
	int y = 200;
	CCMenuItem *pMenu1=CCMenuItemImage::create("tray_guo.png","tray_guo.png",this,menu_selector(LayerTable::onGuoClick)) ;
	pMenu1->setPosition(CCPointMake(size.width-right,y));

	i++;
	if(operateCode&OPERATE_CHI)
	{
		CCMenuItem *pMenu2=CCMenuItemImage::create("tray_word_chi.png","tray_word_chi.png",this,menu_selector(LayerTable::onChiClick)) ;
		pMenu1->setPosition(CCPointMake(size.width-btnW*i-right,y));
		i++;
	}
}

void LayerTable::onGuoClick(CCObject * sender)
{

}
void LayerTable::onChiClick(CCObject * sender)
{

}